package com.example.demo.sensitive.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.sensitive.dto.request.SensitiveWordRequest;
import com.example.demo.sensitive.dto.response.SensitiveWordResponse;
import com.example.demo.sensitive.service.SensitiveWordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

/**
 * @ClassName SensitiveWordController
 * @Description 敏感词管理控制器
 * @Author System
 * @Date 2025/9/19
 **/
@RestController
@RequestMapping("/api/sensitive-words")
@Tag(name = "敏感词管理", description = "敏感词相关操作接口")
public class SensitiveWordController {

    @Resource
    private SensitiveWordService sensitiveWordService;

    @GetMapping
    @Operation(summary = "分页查询敏感词", description = "根据条件分页查询敏感词列表")
    public Result<Page<SensitiveWordResponse>> getPageList(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") int size,
            @Parameter(description = "分类") @RequestParam(required = false) String category,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        
        Page<SensitiveWordResponse> result = sensitiveWordService.getPageList(current, size, category, status, keyword);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询敏感词详情", description = "根据ID查询敏感词详情")
    public Result<SensitiveWordResponse> getById(
            @Parameter(description = "敏感词ID") @PathVariable Long id) {
        
        SensitiveWordResponse result = sensitiveWordService.getById(id);
        return Result.success(result);
    }

    @PostMapping
    @Operation(summary = "创建敏感词", description = "添加新的敏感词")
    public Result<Long> create(@Valid @RequestBody SensitiveWordRequest request) {
        Long id = sensitiveWordService.create(request);
        return Result.success("创建成功", id);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新敏感词", description = "更新指定敏感词信息")
    public Result<String> update(
            @Parameter(description = "敏感词ID") @PathVariable Long id,
            @Valid @RequestBody SensitiveWordRequest request) {
        
        sensitiveWordService.update(id, request);
        return Result.success("更新成功", "success");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除敏感词", description = "删除指定敏感词")
    public Result<String> delete(
            @Parameter(description = "敏感词ID") @PathVariable Long id) {
        
        sensitiveWordService.delete(id);
        return Result.success("删除成功", "success");
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除敏感词", description = "批量删除指定的敏感词")
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        sensitiveWordService.batchDelete(ids);
        return Result.success("批量删除成功", "success");
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新敏感词状态", description = "启用或禁用敏感词")
    public Result<String> updateStatus(
            @Parameter(description = "敏感词ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        
        sensitiveWordService.updateStatus(id, status);
        return Result.success("状态更新成功", "success");
    }

    @PutMapping("/batch/status")
    @Operation(summary = "批量更新状态", description = "批量更新敏感词状态")
    public Result<String> batchUpdateStatus(
            @RequestParam List<Long> ids,
            @RequestParam Integer status) {
        
        sensitiveWordService.batchUpdateStatus(ids, status);
        return Result.success("批量更新成功", "success");
    }

    @GetMapping("/category/{category}")
    @Operation(summary = "按分类查询", description = "根据分类查询敏感词")
    public Result<List<SensitiveWordResponse>> getByCategory(
            @Parameter(description = "分类名称") @PathVariable String category) {
        
        List<SensitiveWordResponse> result = sensitiveWordService.getByCategory(category);
        return Result.success(result);
    }

    @GetMapping("/enabled")
    @Operation(summary = "查询启用的敏感词", description = "查询所有启用状态的敏感词")
    public Result<List<SensitiveWordResponse>> getEnabledWords() {
        List<SensitiveWordResponse> result = sensitiveWordService.getEnabledWords();
        return Result.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "分类统计", description = "统计各分类的敏感词数量")
    public Result<List<SensitiveWordService.CategoryStatistics>> getCategoryStatistics() {
        List<SensitiveWordService.CategoryStatistics> result = sensitiveWordService.getCategoryStatistics();
        return Result.success(result);
    }

    @PostMapping("/batch-import")
    @Operation(summary = "批量导入", description = "从Excel文件批量导入敏感词")
    public Result<SensitiveWordService.ImportResult> batchImport(
            @Parameter(description = "Excel文件") @RequestParam("file") MultipartFile file) {
        
        SensitiveWordService.ImportResult result = sensitiveWordService.batchImport(file);
        return Result.success("导入完成", result);
    }

    @GetMapping("/export")
    @Operation(summary = "导出敏感词", description = "导出敏感词到Excel文件")
    public ResponseEntity<byte[]> export(
            @Parameter(description = "分类") @RequestParam(required = false) String category,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        byte[] data = sensitiveWordService.export(category, status);
        
        String filename = "sensitive_words.xlsx";
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(data);
    }

    @GetMapping("/default-keywords")
    @Operation(summary = "获取默认敏感词库", description = "获取系统预设的默认敏感词库")
    public Result<List<SensitiveWordResponse>> getDefaultKeywords() {
        List<SensitiveWordResponse> result = sensitiveWordService.getDefaultKeywords();
        return Result.success(result);
    }
}
